package com.cheer.ba.presenter.impl;

import android.content.Context;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.EntityCache;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.BreakRulesModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.model.ResultBreak;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 违章查询处理
 *
 * @author HQ
 *         created at 2017/3/3 0003 15:50
 */
public class CarPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;
    private EntityCache<BreakRulesModel> mSaveCarsCache;
    private List<BreakRulesModel> mRulesCache = new ArrayList<BreakRulesModel>();

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
        mSaveCarsCache = new EntityCache<BreakRulesModel>(mContext, BreakRulesModel.class, IntentKey.BreakCar.BREAK_CACHE);
    }

    /**
     * 查询车辆违章信息
     *
     * @param lsprefix 车牌前缀
     * @param lsnum    车牌剩余部分
     * @param frameno  车架号
     */
    public void searchCarInfo(String lsprefix, String lsnum, String frameno) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "");
        Call<ResponseData<BreakRulesModel>> call = mUserService.searchBreakRules(2002, Session.getUserId(), "", "", frameno, "1", lsnum, lsprefix, "");
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<BreakRulesModel>>() {
            @Override
            public void onResponse(Call<ResponseData<BreakRulesModel>> call, Response<ResponseData<BreakRulesModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<BreakRulesModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        BreakRulesModel rules = res.getData();
                        ResultBreak mBreak = rules.showapi_res_body;

//                        if (mBreak.msg.contains("成功")) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateView(mBreak);
//                        } else {
//                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
//                            SVProgressHUD.showInfoWithStatus(mContext, mBreak.msg);
//                        }
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.search_fail));
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.search_fail));
            }

            @Override
            public void onFailure(Call<ResponseData<BreakRulesModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.search_fail));
            }
        });
    }

    @Override
    public void detachView() {
    }

    public void saveCarCache(BreakRulesModel model) {
        mRulesCache.clear();
        List<BreakRulesModel> mBreaks = (List<BreakRulesModel>) getCacheData();
        if (null != mBreaks && !mBreaks.isEmpty()) {
            mRulesCache.addAll(mBreaks);
        }
        mRulesCache.add(model);
        try {
            mSaveCarsCache.putListEntity(mRulesCache);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getCacheData() {
        return mSaveCarsCache.getListEntity(BreakRulesModel.class);
    }

    /**
     * 移除并存储违章信息
     */
    public void delAndSaveCarCache(List<BreakRulesModel> mCars, BreakRulesModel model) {
        mRulesCache.clear();
        if (mCars.size() > 1) {
            mCars.remove(model);
            mRulesCache.addAll(mCars);
            try {
                mSaveCarsCache.putListEntity(mRulesCache);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            mSaveCarsCache.clean(mContext);
        }
        mView.updateViewWithTag(mRulesCache, ConstantTag.DEL_2_SAVE_CAR);
    }
}
